package com.longxi.tianjintianbao.ui.presenter.message;

import android.content.Intent;

import com.longxi.tianjintianbao.AppConst;
import com.longxi.tianjintianbao.MyApplication;
import com.longxi.tianjintianbao.api.ApiRetrofit;
import com.longxi.tianjintianbao.api.response.BaseResponse;
import com.longxi.tianjintianbao.api.response.HttpResult;
import com.longxi.tianjintianbao.exception.ExceptionHandle;
import com.longxi.tianjintianbao.exception.MySubscriber;
import com.longxi.tianjintianbao.listener.AdapterListener;
import com.longxi.tianjintianbao.model.message.MessageList;
import com.longxi.tianjintianbao.notification.MyNotification;
import com.longxi.tianjintianbao.ui.activity.goodstraffic.SignGoodsReleaseActivity;
import com.longxi.tianjintianbao.ui.activity.main.MainActivity;
import com.longxi.tianjintianbao.ui.activity.oa.ChangeFormalDetailActivity;
import com.longxi.tianjintianbao.ui.activity.oa.CopeWithDetailActivity;
import com.longxi.tianjintianbao.ui.activity.oa.CostApplyDetailActivity;
import com.longxi.tianjintianbao.ui.activity.quality.QualityClassListActivity;
import com.longxi.tianjintianbao.ui.activity.rectification.RectificationDetailActivity;
import com.longxi.tianjintianbao.ui.activity.repair.RepairDetailActivity;
import com.longxi.tianjintianbao.ui.activity.task.TaskDetailActivity;
import com.longxi.tianjintianbao.ui.adapter.multitype.message.MessageListViewBinder;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.BasePresenter;
import com.longxi.tianjintianbao.ui.view.message.IMessageAtView;
import com.longxi.tianjintianbao.utils.MyUtils;
import com.longxi.tianjintianbao.utils.NListRefresh;

import org.json.JSONException;
import org.json.JSONObject;

import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class MessageAtPresenter extends BasePresenter<IMessageAtView> {

    private MultiTypeAdapter adapter;
    private Items items;
    private String title;
    private int billtype;

    public MessageAtPresenter(BaseActivity context) {
        super(context);
    }

    public void setBar() {
        title = mContext.getIntent().getStringExtra(AppConst.NAME);
        mContext.setTvLeft(title);
        billtype = mContext.getIntent().getIntExtra(AppConst.STATE, 0);
    }

    public void initAdapter(){
        if(adapter == null){
            adapter = new MultiTypeAdapter();
            /* 注册类型和 View 的对应关系 */
            adapter.register(MessageList.class, new MessageListViewBinder(listener, title));
            getView().getRvContent().setAdapter(adapter);
        }
    }

    /**
     * 获取详情数据
     */
    public void getMessageList(int page){
        JSONObject object = new JSONObject();
        try {
            object.put("userid", MyApplication.loginUser.userid);
            object.put("billtype", billtype);
            object.put("page", page);
            object.put("rowcount", 10);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ApiRetrofit.getInstance().getMessageList(object)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new MySubscriber<HttpResult<MessageList>>(mContext) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable throwable) {
                mContext.hideWaitingDialog();
                MyUtils.showToast(throwable.message);
            }

            @Override
            public void onNext(HttpResult<MessageList> result) {
                items = NListRefresh.getInstance().fjData(result,
                        getView().getRefreshLayout(),
                        items,
                        adapter,
                        mContext);
            }
        });
    }

    AdapterListener listener = new AdapterListener() {
        @Override
        public void onClick(int position) {
            MessageList messageList = (MessageList) items.get(position);

            if(messageList.getLookover() == 0){
                JSONObject object = new JSONObject();
                try {
                    object.put("userid", MyApplication.loginUser.userid);
                    object.put("billtype", billtype);
                    object.put("messageid", messageList.getMessageid());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                ApiRetrofit.getInstance().saveMessageLookOver(object)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new MySubscriber<BaseResponse>(mContext) {
                    @Override
                    public void onError(ExceptionHandle.ResponeThrowable throwable) {
                        mContext.hideWaitingDialog();
                        MyUtils.showToast(throwable.message);
                    }

                    @Override
                    public void onNext(BaseResponse result) {
                        if(result.getCode() == 0){
                            messageList.setLookover(1);
                            adapter.notifyItemChanged(position);
                            MainActivity.mainActivity.refreshMessage();
                        }
                        MyNotification.getMyNotificationInstance().jump(mContext, messageList.getBillid(), billtype);
                    }
                });
            }else{
                MyNotification.getMyNotificationInstance().jump(mContext, messageList.getBillid(), billtype);
            }


        }
        @Override
        public void onDelete(int position) {

        }
    };

    public void onDestroy() {
        listener = null;
    }

}
